package com.blueocean_health.care.management.controller;

import java.io.BufferedOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.net.URLEncoder;
import java.util.Date;
import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.blueocean_health.care.management.common.LocalManager;
import com.blueocean_health.care.management.common.base.em.TimeType;
import com.blueocean_health.care.management.common.base.string.StringUtils;
import com.blueocean_health.care.management.common.base.time.TimeUtils;
import com.blueocean_health.care.management.common.excel.ExcelUtil;
import com.blueocean_health.care.management.domain.base.SysResult;
import com.blueocean_health.care.management.domain.base.User;
import com.blueocean_health.care.management.domain.dto.DepartmentCountReport;
import com.blueocean_health.care.management.domain.dto.DepartmentDailyReport;
import com.blueocean_health.care.management.domain.ordervo.DepartmentReportVo;
import com.blueocean_health.care.management.domain.worktablevo.NurseWokerStatistics;
import com.blueocean_health.care.management.em.LocalManagerType;
import com.blueocean_health.care.management.service.ReportService;

/**
 * @author			作者:            zwg.BlueOcean
 * @date       			创建时间：2017年9月1日  上午9:45:15
 * @version  		版本号：    1.0*
 * @parameter  	* 
 * @since  			* 
 * @return  			*
 * @description	*	
 */
@RestController
@RequestMapping(produces = MediaType.APPLICATION_JSON_VALUE)
public class DepartmentReportController {
	@Autowired
	private ReportService reportService;
	/**
	 * 科室结算明细表  2017年9月5日10:16:11
	 * @param request
	 * @param dvo
	 * @return
	 * @throws Exception
	 */
	@RequestMapping("/web/department/daily-report")
	public SysResult dailyQuery(HttpServletRequest request,@RequestBody DepartmentReportVo dvo) throws Exception{
		User courrentUser = LocalManager.getVal(LocalManagerType.USER_WEB);
		String hospitalId=courrentUser.getHospitalId();
		//数据处理
		//校验日期格式
		if(StringUtils.isEmpty(dvo.getBeginTime())){
			dvo.setBeginTime(TimeUtils.getStringByDate(new Date(), TimeType.yyyy_MM_dd));
		}
		if(StringUtils.isEmpty(dvo.getEndTime()) ){
			dvo.setEndTime(TimeUtils.getStringByDate(new Date(), TimeType.yyyy_MM_dd));
		}
		if( !dvo.getBeginTime().matches("\\d{4}-\\d{1,2}-\\d{1,2}")){
			throw new Exception("查询开始日期不符合规范{yyyy-MM-dd}");
		}
		if(!dvo.getEndTime().matches("\\d{4}-\\d{1,2}-\\d{1,2}")){
			throw new Exception("查询结束日期不符合规范{yyyy-MM-dd}");
		}
		return reportService.getDepartmentDailyReport(dvo, hospitalId);
	}
	/**
	 * 科室结算明细表  导出Excel
	 * @param request
	 * @param response
	 * @throws Exception
	 */
	@RequestMapping("/web/department/daily-report/excel")
	public void getDeptDailyExcel(HttpServletRequest request,
			HttpServletResponse response) throws Exception{
		try{
			User courrentUser = LocalManager.getVal(LocalManagerType.USER_WEB);
			String hospitalId=courrentUser.getHospitalId();
			DepartmentReportVo dvo=new DepartmentReportVo();
			dvo.setBeginTime(request.getParameter("beginTime"));
			dvo.setEndTime(request.getParameter("endTime"));
			dvo.setServiceType(request.getParameter("serviceType"));
			String dept=request.getParameter("department");
			dvo.setDepartment(StringUtils.isEmpty(dept)?null:Integer.valueOf(dept));
			//数据处理
			//校验日期格式
			if(StringUtils.isEmpty(dvo.getBeginTime())){
				dvo.setBeginTime(TimeUtils.getStringByDate(new Date(), TimeType.yyyy_MM_dd));
			}
			if(StringUtils.isEmpty(dvo.getEndTime()) ){
				dvo.setEndTime(TimeUtils.getStringByDate(new Date(), TimeType.yyyy_MM_dd));
			}
			if(!dvo.getBeginTime().matches("\\d{4}-\\d{1,2}-\\d{1,2}")){
				throw new Exception("查询开始日期不符合规范{yyyy-MM-dd}");
			}
			if( !dvo.getEndTime().matches("\\d{4}-\\d{1,2}-\\d{1,2}")){
				throw new Exception("查询结束日期不符合规范{yyyy-MM-dd}");
			}
			NurseWokerStatistics nws=new NurseWokerStatistics();
			List<DepartmentDailyReport> results = reportService.findDeptDailyListData(dvo, hospitalId,nws ,true);
			String finalFileName = URLEncoder.encode("科室结算明细表_"+TimeUtils.getTime(TimeType.yyyyMMddHHmmssSSS)+".xls", "UTF8");
			response.addHeader("Content-Disposition", "attachment;filename=" + finalFileName);
			OutputStream toClient = new BufferedOutputStream(response.getOutputStream());
			
			ExcelUtil.getInstance().exportObj2Excel(toClient, results, DepartmentDailyReport.class);
		}catch(IOException e){
			e.printStackTrace();
		}
		
	}	
	/**
	 * 科室结算汇总表 
	 * @param request
	 * @param dvo
	 * @return
	 * @throws Exception
	 */
	@RequestMapping("/web/department/count-report")
	public SysResult countQuery(HttpServletRequest request,@RequestBody DepartmentReportVo dvo) throws Exception{
		User courrentUser = LocalManager.getVal(LocalManagerType.USER_WEB);
		String hospitalId=courrentUser.getHospitalId();
		//数据处理
		//校验日期格式
		if(StringUtils.isEmpty(dvo.getBeginTime())){
			dvo.setBeginTime(TimeUtils.getStringByDate(new Date(), TimeType.yyyy_MM_dd));
		}
		if(StringUtils.isEmpty(dvo.getEndTime()) ){
			dvo.setEndTime(TimeUtils.getStringByDate(new Date(), TimeType.yyyy_MM_dd));
		}
		if(!dvo.getBeginTime().matches("\\d{4}-\\d{1,2}-\\d{1,2}")){
			throw new Exception("查询开始日期不符合规范{yyyy-MM-dd}");
		}
		if(!dvo.getEndTime().matches("\\d{4}-\\d{1,2}-\\d{1,2}")){
			throw new Exception("查询结束日期不符合规范{yyyy-MM-dd}");
		}
		return reportService.getDepartmentCountReport(dvo, hospitalId);
	}
	/**
	 * 科室结算汇总表 导出excel
	 * @param request
	 * @param response
	 * @throws Exception
	 */
	@RequestMapping("/web/department/count-report/excel")
	public void getDeptCountExcel(HttpServletRequest request,
			HttpServletResponse response) throws Exception{
		try{
			User courrentUser = LocalManager.getVal(LocalManagerType.USER_WEB);
			String hospitalId=courrentUser.getHospitalId();
			DepartmentReportVo dvo=new DepartmentReportVo();
			dvo.setBeginTime(request.getParameter("beginTime"));
			dvo.setEndTime(request.getParameter("endTime"));
			String dept=request.getParameter("department");
			dvo.setDepartment(StringUtils.isEmpty(dept)?null:Integer.valueOf(dept));
			//数据处理
			//校验日期格式
			if(StringUtils.isEmpty(dvo.getBeginTime())){
				dvo.setBeginTime(TimeUtils.getStringByDate(new Date(), TimeType.yyyy_MM_dd));
			}
			if(StringUtils.isEmpty(dvo.getEndTime()) ){
				dvo.setEndTime(TimeUtils.getStringByDate(new Date(), TimeType.yyyy_MM_dd));
			}
			if( !dvo.getBeginTime().matches("\\d{4}-\\d{1,2}-\\d{1,2}")){
				throw new Exception("查询开始日期不符合规范{yyyy-MM-dd}");
			}
			if( !dvo.getEndTime().matches("\\d{4}-\\d{1,2}-\\d{1,2}")){
				throw new Exception("查询结束日期不符合规范{yyyy-MM-dd}");
			}
			//导出excel 如果传入null 不实例化对象，会报你nullException异常 2017年9月6日15:42:57
			NurseWokerStatistics nws=new NurseWokerStatistics();
			List<DepartmentCountReport> results = reportService.findDeptCountListData(dvo, hospitalId,nws);
			String finalFileName = URLEncoder.encode("科室结算汇总表_"+TimeUtils.getTime(TimeType.yyyyMMddHHmmssSSS)+".xls", "UTF8");
			response.addHeader("Content-Disposition", "attachment;filename=" + finalFileName);
			OutputStream toClient = new BufferedOutputStream(response.getOutputStream());
			ExcelUtil.getInstance().exportObj2Excel(toClient, results, DepartmentCountReport.class);
		}catch(IOException e){
			e.printStackTrace();
		}
		
	}

}

